package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.SetUserPasswordModel;

public class SetPasswordActivity extends BaseActivity implements View.OnClickListener {

    EditText setpassword_oldpassword,setpassword_newpassword,setpassword_newpassword1;
    ImageView setpassword_fh;
    ImageView iv_see_password3,iv_see_password4,iv_see_password5;

    Button setpassword_btn;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_set_password);
        setpassword_oldpassword = (EditText)findViewById(R.id.setpassword_oldpassword);
        setpassword_newpassword = (EditText)findViewById(R.id.setpassword_newpassword);
        setpassword_newpassword1 = (EditText)findViewById(R.id.setpassword_newpassword1);
        iv_see_password3 = (ImageView)findViewById(R.id.iv_see_password3);
        iv_see_password4 = (ImageView)findViewById(R.id.iv_see_password4);
        iv_see_password5 = (ImageView)findViewById(R.id.iv_see_password5);

        setpassword_fh = (ImageView)findViewById(R.id.setpassword_fh);
        setpassword_btn = (Button)findViewById(R.id.setpassword_btn);



    }

    @Override
    void initEvent() {
        setpassword_btn.setOnClickListener(this);
        setpassword_fh.setOnClickListener(this);
        iv_see_password3.setOnClickListener(this);
        iv_see_password4.setOnClickListener(this);
        iv_see_password5.setOnClickListener(this);
    }

    @Override
    void initDate() {

    }
    private void setPasswordVisibility(){
        if (iv_see_password3.isSelected()) {
            iv_see_password3.setSelected(false);
            setpassword_oldpassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        } else {
            iv_see_password3.setSelected(true);
            setpassword_oldpassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
    }
    private void setPasswordVisibility4(){
        if (iv_see_password4.isSelected()) {
            iv_see_password4.setSelected(false);
            setpassword_newpassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        } else {
            iv_see_password4.setSelected(true);
            setpassword_newpassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
    }
    private void setPasswordVisibility5(){
        if (iv_see_password5.isSelected()) {
            iv_see_password5.setSelected(false);
            setpassword_newpassword1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        } else {
            iv_see_password5.setSelected(true);
            setpassword_newpassword1.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.setpassword_btn:
                String oldpassword = setpassword_oldpassword.getText().toString().trim();
                String newpassword = setpassword_newpassword.getText().toString().trim();
                String newpassword1 = setpassword_newpassword1.getText().toString().trim();
                String userid =  getid();
                if (oldpassword.equals("")||newpassword.equals("")||newpassword1.equals("")){
                    Toast.makeText(SetPasswordActivity.this,"输入信息有空值",Toast.LENGTH_SHORT).show();
                }
                else if(!newpassword.equals(newpassword1)){
                    Toast.makeText(SetPasswordActivity.this,"两次新密码不一致",Toast.LENGTH_SHORT).show();
                }
                else{
                    SetUserPasswordModel setUserPasswordModel = new SetUserPasswordModel();
                    setUserPasswordModel.setUserPassword(userid,oldpassword,newpassword,successBeanTListener);
                }
                break;
            case R.id.setpassword_fh:
                finish();
                break;
            case R.id.iv_see_password3:
                setPasswordVisibility();
                break;
            case R.id.iv_see_password4:
                setPasswordVisibility4();
                break;
            case R.id.iv_see_password5:
                setPasswordVisibility5();
                break;

        }
    }

    TListener<SuccessBean> successBeanTListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            String success = successBean.getSuccess();

            if (success.equals("1")){
               Toast.makeText(SetPasswordActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                finish();
            }
            else if(success.equals("2")){
                Toast.makeText(SetPasswordActivity.this,"旧密码输入错误，修改失败",Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(SetPasswordActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(SetPasswordActivity.this,"修改失败1",Toast.LENGTH_SHORT).show();
        }
    };
}
